package tankgame01;

import java.util.Vector;

public class EnemyTank extends Tank implements Runnable {
    Vector<BulletShot> bulletShots = new Vector<>();
    BulletShot bulletShot = null;
    Vector<EnemyTank> enemyTanks = new Vector<>();
    ShotRun shotRun = new ShotRun(this);

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    public void shot() {
            switch (getDirection()) {
                case 0:
                    bulletShot = new BulletShot(getX() + 20, getY(), 0);
                    bulletShots.add(bulletShot);
                    break;
                case 1:
                    bulletShot = new BulletShot(getX() + 60, getY() + 20, 1);
                    bulletShots.add(bulletShot);
                    break;
                case 2:
                    bulletShot = new BulletShot(getX() + 20, getY() + 60, 2);
                    bulletShots.add(bulletShot);
                    break;
                case 3:
                    bulletShot = new BulletShot(getX(), getY() + 20, 3);
                    bulletShots.add(bulletShot);
                    break;
            }

            bulletShots.add(bulletShot);
            bulletShot.setSpeed(20);
            new Thread(bulletShot).start();
    }

    public boolean isTouch() {
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            if (enemyTank != this) {
                switch (enemyTank.getDirection()) {
                    case 0:
                    case 2:
                        if (this.getDirection() == 0) {
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (this.getDirection() == 1) {
                            if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (this.getDirection() == 2) {
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (this.getDirection() == 3) {
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        break;
                    case 1:
                    case 3:
                        if (this.getDirection() == 0) {
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                        if (this.getDirection() == 1) {
                            if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                        if (this.getDirection() == 2) {
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                        if (this.getDirection() == 3) {
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                        break;
                }
            }
        }
        return false;
    }

    @Override
    public void run() {
        shotRun.start();
        while (true) {
            for (int i = 0; i < 30; i++) {
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (!(isTouch())) {
                    switch (getDirection()) {
                        case 0:
                            moveUp();
                            break;
                        case 1:
                            moveRight();
                            break;
                        case 2:
                            moveDown();
                            break;
                        case 3:
                            moveLeft();
                            break;
                    }
                }
            }
            setDirection((int) (Math.random() * 4));
            try {
                Thread.sleep(150);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
//            if (bulletShots.size() <= 10) {
//                bulletShot();
//            }
            if (!isLive) {
                break;
            }
        }
    }

}
